﻿
app.controller('ConstructionController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.GetFormStatusList = function (isshow) {
        $scope.PageLoading = true;
        $scope.StateList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetFormStatusList'
        }).success(function (ret) {
            $scope.PageLoading = false;
            if (ret.Code == 100) {
                $scope.StateList = ret.Data;
                if (isshow) {
                    $scope.slist = [];
                    for (var i = 0; i < $scope.StateList.length; i++) {
                        $scope.slist.push(ret.Data[i].StatusName);
                    }
                    $scope.ShowStep();
                }
                
            } else
                alertify.alert(ret.Msg);
        }).error(function (data, status, headers, config) {
            common.error($scope);
            $scope.PageLoading = false;
            alertify.alert("请检查网络");
        })
    }

    $scope.ShowStep = function (State) {
        $("#step").html("");
        $scope.MyStep = $("#step");
        $scope.MyStep.step({
            index: -1,
            time: 500,
            title: $scope.slist
        });
        if (State != null) {
            $scope.MyStep.toStep(State - 1);
        }
    }
    $scope.GetConstructionType = function () {
        $scope.PageLoading = true;
        $scope.TypeList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionType'
        }).success(function (ret) {
            $scope.PageLoading = false;
            if (ret.Code == 100) {
                $scope.TypeList = ret.Data;
            } else
                alertify.alert(ret.Msg);
        }).error(function (data, status, headers, config) {
            common.error($scope);
            $scope.PageLoading = false;
            alertify.alert("请检查网络");
        })
    }

    $scope.GetConstructionLevel = function () {
        $scope.PageLoading = true;
        $scope.LevelList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionLevel'
        }).success(function (ret) {
            $scope.PageLoading = false;
            if (ret.Code == 100) {
                $scope.LevelList = ret.Data;
            } else
                alertify.alert(ret.Msg);
        }).error(function (data, status, headers, config) {
            common.error($scope);
            $scope.PageLoading = false;
            alertify.alert("请检查网络");
        })
    }

    $scope.GetMyDepartmentLeader = function () {
        $scope.PageLoading = true;
        $scope.LeaderList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetDepartmentLeaderList'
        }).success(function (ret) {
            $scope.PageLoading = false;
            if (ret.Code == 100) {
                $scope.LeaderList = ret.Data;
            } else
                alertify.alert(ret.Msg);
        }).error(function (data, status, headers, config) {
            common.error($scope);
            $scope.PageLoading = false;
            alertify.alert("请检查网络");
        })
    }

    $scope.GetMyDepartmentList = function () {
        $scope.PageLoading = true;
        $scope.DepList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetMyDepartmentList'
        }).success(function (ret) {
            $scope.PageLoading = false;
            if (ret.Code == 100) {
                $scope.DepList = ret.Data;
            } else
                alertify.alert(ret.Msg);
        }).error(function (data, status, headers, config) {
            common.error($scope);
            $scope.PageLoading = false;
            alertify.alert("请检查网络");
        })
    }

    $scope.GetConstructionRisk = function (Id,defaultvalue) {
        if (Id == null || Id == "") {
            $scope.RiskList = [];
            return false;
        }
        $scope.PageLoading = true;
        $scope.RiskList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionRisk',
            data: { "TypeId": Id }
        }).success(function (ret) {
            $scope.PageLoading = false;
            if (ret.Code == 100) {
                $scope.RiskList = ret.Data;
                if (defaultvalue != null) {
                    setTimeout(function () {
                        for (let i = 0; i < defaultvalue.length; i++) {
                            $("#risk_" + defaultvalue[i]).prop("checked", true);
                        }
                    }, 500);
                }
            } else
                alertify.alert(ret.Msg);
        }).error(function (data, status, headers, config) {
            common.error($scope);
            $scope.PageLoading = false;
            alertify.alert("请检查网络");
        })
    }

    $scope.GetConstructionSolution = function (Id, defaultvalue) {
        if (Id == null || Id=="") {
            $scope.SolutionList = [];
            return false;
        }
        $scope.PageLoading = true;
        $scope.SolutionList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionSolution',
            data: { "TypeId": Id }
        }).success(function (ret) {
            $scope.PageLoading = false;
            if (ret.Code == 100) {
                $scope.SolutionList = ret.Data;
                if (defaultvalue != null) {
                    setTimeout(function () {
                        for (let i = 0; i < defaultvalue.length; i++) {
                            $("#solution_" + defaultvalue[i]).prop("checked", true);
                        }
                    }, 500);
                }
              
            } else
                alertify.alert(ret.Msg);
        }).error(function (data, status, headers, config) {
            common.error($scope);
            $scope.PageLoading = false;
            alertify.alert("请检查网络");
        })
    }

    $scope.GetConstructionSafeEqu = function (Id, defaultvalue) {
        if (Id == null || Id == "") {
            $scope.EquList = [];
            return false;
        }
        $scope.PageLoading = true;
        $scope.EquList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionSafetyEqu',
            data: { "TypeId": Id }
        }).success(function (ret) {
            $scope.PageLoading = false;
            if (ret.Code == 100) {
                $scope.EquList = ret.Data;
                if (defaultvalue != null) {
                    setTimeout(function () {
                        for (let i = 0; i < defaultvalue.length; i++) {
                            $("#equ_" + defaultvalue[i]).prop("checked", true);
                        }
                    }, 1000);
                }
               
            } else
                alertify.alert(ret.Msg);
        }).error(function (data, status, headers, config) {
            common.error($scope);
            $scope.PageLoading = false;
            alertify.alert("请检查网络");
        })
    }


    $scope.AddConstructionCompany = function (model) {
        model.Process = 100;
        $scope.DataLoading = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        $http({
            method: 'POST',
            url: '../Construction/AddConstructionCompany',
            data: { 'model': model, "Leader": model.DepLeader },
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.AlertBox("外协单位添加成功");
                    $scope.BindConstructionCompanyList("sel_companylist");
                    $("#btn_close_1").click();
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }
    $scope.AddConstructionWorker = function (model) {
        $scope.DataLoading = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        $http({
            method: 'POST',
            url: '../Construction/AddConstructionWorkers',
            data: { 'model': model },
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.AlertBox("作业人添加成功");
                    $scope.BindConstructionWorkerList("sel_workerlist");
                    $("#btn_close_2").click();
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }
    $scope.refreshOption = function (control) {
        $("#" + control).selectpicker('refresh');
    }
    $scope.AddConstructionCaretaker = function (model) {
        $scope.DataLoading = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        $http({
            method: 'POST',
            url: '../Construction/AddConstructionCaretaker',
            data: { 'model': model },
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.AlertBox("监护人添加成功");
                    $scope.BindConstructionCaretaker("caretakerlist");
                    $("#btn_close_3").click();
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }
    $scope.BindConstructionCompanyList = function (control, defaultvalue) {
        $scope.PageLoading = true;
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionCompany'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (defaultvalue != null) {
                            if (n.Id == defaultvalue)
                                $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.CompanyName + "</option>")($scope));
                            else
                                $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.CompanyName + "</option>")($scope));
                        }
                        else {
                            $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.CompanyName + "</option>")($scope));
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.MGetConstructionCompanyList = function () {
        $scope.PageLoading = true;
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionCompany'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.OutCompanyList = ret.Data;
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.MGetWorkerList = function () {
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionWorkers'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.WorkerList = ret.Data;
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.BindConstructionWorkerList = function (control, defaultvalue) {
        $scope.PageLoading = true;
        $("#" + control).empty();
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionWorkers'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (defaultvalue != null) {
                            if (defaultvalue.includes(n.Id+""))
                                $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.WorkerName + "-" + n.WorkerPhone + "</option>")($scope));
                            else
                                $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.WorkerName + "-" + n.WorkerPhone + "</option>")($scope));
                        }
                        else {
                            $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.WorkerName + "-" + n.WorkerPhone + "</option>")($scope));
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetAllUserList = function (control, defaultvalue) {
        $scope.PageLoading = true;
        $http({
            method: 'POST',
            url: '../Construction/GetAllUserList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (defaultvalue != null) {
                            if (defaultvalue.includes(n.Id + ""))
                                $("#" + control).append($compile("<option selected='selected' value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                            else
                                $("#" + control).append($compile("<option value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                        }
                        else {
                            $("#" + control).append($compile("<option value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetUserByName = function (name) {
        $scope.resultlist = [];
        for (let i = 0; i < $scope.AllUserList.length; i++) {
            if ($scope.AllUserList[i].UserName.includes(name) || $scope.AllUserList[i].UserNo.includes(name)) {
                $scope.resultlist.push($scope.AllUserList[i]);
            }
            if ($scope.resultlist.length > 5) {
                break;
            }
        }
    }

    $scope.selectuser = function (model) {
        $scope.name = model.UserNo + "-" + model.UserName;
        $scope.resultlist = [];
    }

    $scope.GetAllUserList = function () {
        if ($scope.AllUserList != null && $scope.AllUserList.length > 0) {
            return false;
        }
        $scope.AllUserList = [];
        $scope.PageLoading = true;
        $http({
            method: 'POST',
            url: '../Construction/GetAllUserList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.AllUserList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.BindConstructionCaretaker = function (control, defaultvalue) {
        $scope.PageLoading = true;
        $("#" + control).empty();
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionCaretaker'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (defaultvalue != null) {
                            if (defaultvalue.includes(n.Id+""))
                                $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.CaretakerName + "-" + n.CaretakerPhone + "</option>")($scope));
                            else
                                $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.CaretakerName + "-" + n.CaretakerPhone + "</option>")($scope));
                        }
                        else {
                            $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.CaretakerName + "-" + n.CaretakerPhone + "</option>")($scope));
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.GetParkList = function () {
        $scope.ParkList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetParkList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.ParkList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetPlaceList = function (control, id, defaultvalue) {
        if (id == null || id == "")
            return false;
        $("#" + control).empty();
        $("#" + control).selectpicker('refresh');
        $http({
            method: 'POST',
            url: '../IssueReport/GetParkPlace',
            data: { "ParkId": id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (defaultvalue != null) {
                            if (n.Id == defaultvalue)
                                $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.PlaceName + "</option>")($scope));
                            else
                                $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.PlaceName + "</option>")($scope));
                        }
                        else {
                            $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.PlaceName + "</option>")($scope));
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.AddConstructionForm = function (model,status) {
        var riskcontrol = $("input[id^='risk_']");
        var riskid = [];
        for (let i = 0; i < riskcontrol.length; i++) {
            if ($(riskcontrol[i]).prop("checked")) {
                riskid.push(riskcontrol[i].id.replace("risk_",''));
            }
        }

        var solutioncontrol = $("input[id^='solution_']");
        var solutionid = [];
        for (let i = 0; i < solutioncontrol.length; i++) {
            if ($(solutioncontrol[i]).prop("checked")) {
                solutionid.push(solutioncontrol[i].id.replace("solution_", ''));
            }
        }

        var equcontrol = $("input[id^='equ_']");
        var equid = [];
        for (let i = 0; i < equcontrol.length; i++) {
            if ($(equcontrol[i]).prop("checked")) {
                equid.push(equcontrol[i].id.replace("equ_", ''));
            }
        }

        if (riskid.length != 0) {
            model.RiskId = riskid.join(",");
        }
        if (solutionid.length != 0) {
            model.SolutionId = solutionid.join(",");
        }
        if (equid.length != 0) {
            model.SafetyEquId = equid.join(",");
        }
        if (model.Worker != null && model.Worker.length > 0) {
            model.WorkerId = model.Worker.join(",");
        }
        if (model.Caretakers != null && model.Caretakers.length > 0) {
            model.Caretaker = model.Caretakers.join(",");
        }
        if (model.Place != null && model.Place.length > 0) {
            model.PlaceId = model.Place.join(",");
        }
        if (model.DepLeader == null && status == 1) {
            alertify.alert("请选择部门审核人");
            return false;
        }

        model.Status = status;
        var files = document.getElementById("docfiles").files;
        $scope.Submiting = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        Upload.upload({
            method: 'POST',
            url: '../Construction/AddConstructionForm',
            data: { 'model': model, "Leader":model.DepLeader },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    location.href = "ConstructionFormPageList";
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })

        console.log(model);
    }

    $scope.InitOperate = function (model) {
        $scope.btnlist = [];
        if (model != null) {
            if (model.IsShowApprove) {
                var obj_approve = {
                    "name": "审核",
                    "link":"../Construction/MApprovePage?FormId="+model.Id
                };
                $scope.btnlist.push(obj_approve);
            }
            var obj_detail = {
                "name": "详细",
                "link": "../Construction/MConstructionDetail?FormId=" + model.Id
            };
            $scope.btnlist.push(obj_detail);
        }
        mui('#operate').popover('toggle');
    }

    $scope.InitConstructionPageList = function () {
        $scope.PageIndex = 1;
        mui.init({
            pullRefresh: {
                container: '#pullrefresh',
                down: {
                    style: 'circle',
                    auto: true,//可选,默认false.首次加载自动上拉刷新一次
                    callback: function () {
                        setTimeout(function () {
                            $scope.MGetConstructionPageList($scope.SearchModel, 1);
                            mui('#pullrefresh').pullRefresh().refresh(true);
                            mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
                        }, 1500);

                    }
                }, up: {
                    auto: false,
                    contentrefresh: '正在加载...',
                    callback: function () {
                        $scope.MGetConstructionPageList($scope.SearchModel,$scope.PageIndex + 1);
                    },

                }
            }
        });
    }

    $scope.MGetConstructionPageList = function (model, page) {
        if (page == 1) {
            $scope.DataList = [];
        }
        $scope.PageIndex = page;
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionPageList',
            data: { 'model': model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    for (var i = 0; i < ret.Data.Data.length; i++) {
                        $scope.DataList.push(ret.Data.Data[i]);
                    }
                    if (ret.Data.Data.length == 0) {
                        mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);
                    }
                    else {
                        mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
                    }
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetConstructionPageList = function (model, page, isload) {
        $scope.DataLoading = true;
        $scope.DataList = [];
        var condition = JSON.parse(sessionStorage.getItem(window.location.pathname));
        if (isload && condition) {
            model = condition;
            $scope.SearchModel = condition;
        } else {
            sessionStorage.setItem(window.location.pathname, JSON.stringify(model));
        }
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionPageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetConstructionDetail = function (Id) {
        $scope.DataLoading = true;
        $scope.Contruction = {};
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionDetail',
            data: { "Id": Id },
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.Contruction = ret.Data;
                    if (ret.Data.RiskId != null) {
                        $scope.GetConstructionRisk(ret.Data.TypeId, ret.Data.RiskId.split(','));
                    } else {
                        $scope.GetConstructionRisk(ret.Data.TypeId);
                    }

                    if (ret.Data.SolutionId != null) {
                        $scope.GetConstructionSolution(ret.Data.TypeId, ret.Data.SolutionId.split(','));
                    } else {
                        $scope.GetConstructionSolution(ret.Data.TypeId);
                    }

                    if (ret.Data.SafetyEquId != null) {
                        $scope.GetConstructionSafeEqu(ret.Data.TypeId, ret.Data.SafetyEquId.split(','));
                    }
                    else {
                        $scope.GetConstructionSafeEqu(ret.Data.TypeId);
                    }
                    
                    $scope.BindConstructionCompanyList('sel_companylist', $scope.Contruction.OutCompanyId);
                    $scope.BindConstructionWorkerList('sel_workerlist', ret.Data.WorkerId.split(','));
                    $scope.BindConstructionCaretaker('caretakerlist', ret.Data.Caretaker.split(','));                    
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.MGetConstructionDetail = function (Id) {
        mui.showLoading("正在加载..", "div");
        $scope.DataLoading = true;
        $scope.Construction = {};
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionDetail',
            data: { "Id": Id },
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                mui.hideLoading();
                if (ret.Code == 100) {
                    $scope.Construction = ret.Data;
                    $scope.Construction.OutCompanyId = $scope.Construction.OutCompanyId + "";
                    if (ret.Data.RiskId != null) {
                        $scope.GetConstructionRisk(ret.Data.TypeId, ret.Data.RiskId.split(','));
                    } else {
                        $scope.GetConstructionRisk(ret.Data.TypeId);
                    }

                    if (ret.Data.SolutionId != null) {
                        $scope.GetConstructionSolution(ret.Data.TypeId, ret.Data.SolutionId.split(','));
                    } else {
                        $scope.GetConstructionSolution(ret.Data.TypeId);
                    }

                    if (ret.Data.SafetyEquId != null) {
                        $scope.GetConstructionSafeEqu(ret.Data.TypeId, ret.Data.SafetyEquId.split(','));
                    }
                    else {
                        $scope.GetConstructionSafeEqu(ret.Data.TypeId);
                    }
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                mui.hideLoading();
                $scope.DataLoading = false;
                mui.alert("请检查网络");
            });
    }

    $scope.MApproveConstruction = function (approvemodel, status) {
        approvemodel.IsApprove = status;
        var text = "驳回";
        if (approvemodel.IsApprove) {
            text = "同意";
            if (approvemodel.NextStatus == null || approvemodel.NextStatus == "") {
                mui.alert("请选择下一节点");
                return false;
            }
         
            if ($scope.IsShowChoose) {
                var userdata = $scope.name.split('-');
                if (userdata.length != 2) {
                    mui.alert("请选择下一节点审批人");
                    return false;
                }
                approvemodel.NextUser = userdata[0];
            }
        }
        mui.confirm("确认" + text + "该申请单？", '系统提醒', function (e) {
            if (e.index ==1) {
                $http({
                    method: 'POST',
                    url: '../Construction/ApproveConstructionForm',
                    data: { 'model': approvemodel },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            mui.toast(ret.Msg);
                            setTimeout(function () {
                                location.href = "MConstructionPageList";
                            }, 2000);
                        } else
                            mui.toast(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }
        })
    }

    $scope.ApproveConstruction = function (approvemodel, status, page) {
        approvemodel.IsApprove = status;
        var text = "驳回";
        if (approvemodel.IsApprove) {
            text = "同意";
            if (approvemodel.NextStatus == null || approvemodel.NextStatus == "") {
                alertify.alert("请选择下一节点");
                return false;
            }

            if ($scope.IsShowChoose && approvemodel.NextUser == null)
            {
                alertify.alert("请选择下一节点审批人");
                return false;
            }
        }
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认" + text + "该申请单？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../Construction/ApproveConstructionForm',
                    data: { 'model': approvemodel},
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                location.href = "ConstructionFormPageList?page="+page;
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();
                
            });
    }
    
    $scope.GetApproveList = function (Id) {
        $scope.ApproveList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetApproveList',
            data: { "Id": Id}
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.ApproveList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.GetApproveNodes = function (status) {
        $scope.ApproveNodeList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetApproveNodes',
            data: { "Status": status }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.ApproveNodeList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    //$scope.GetFormStatusList = function () {
    //    $scope.StatusList = [];
    //    $http({
    //        method: 'POST',
    //        url: '../Construction/GetFormStatusList'
    //    })
    //        .success(function (ret) {
    //            common.success($scope, null, ret);
    //            if (ret.Code == 100) {
    //                $scope.StatusList = ret.Data;
    //            }
    //            else {
    //                alertify.alert(ret.Msg);
    //            }
    //        })
    //        .error(function (data, status, headers, config) {
    //            alertify.alert("请检查网络");
    //        });
    //}

    $scope.DeleteConstruction = function (Id,model,page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该申请单？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../Construction/DeleteConstruction',
                    data: { 'Id': Id},
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            $scope.GetConstructionPageList(model,page);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.RecallConstruction = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认撤回该申请单？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../Construction/RecallConstruction',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            $scope.GetConstructionPageList(model, page);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }
    $scope.DeleteConstructionFile = function (Id, filelist) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../Construction/DeleteConstructionFile',
                    data: { 'Id': Id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        if (ret.Code == 100) {
                            for (var i = 0; i < filelist.length; i++) {
                                if (filelist[i].FileId == Id) {
                                    filelist.splice(i, 1);
                                }
                            }
                            alertify.alert("删除成功！");
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        $scope.PageLoading = false;
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.IsShowChooseUser = function (id, list) {
        $scope.IsShowChoose = false;
        for (var i = 0; i < list.length; i++) {
            if (list[i].NextStatus==id)
            {
                $scope.IsShowChoose = list[i].ChooseUser
            }
        }
    }

    $scope.GetConstructionEdit = function (Id) {
        $scope.DataLoading = true;
        $scope.Contruction = {};
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionDetail',
            data: { "Id": Id },
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.Contruction = ret.Data;
                    if (ret.Data.ParkId != null) {
                        $scope.Contruction.ParkId = $scope.Contruction.ParkId + "";
                        $scope.GetPlaceList('placelist', $scope.Contruction.ParkId, $scope.Contruction.PlaceId);
                    }
                    if (ret.Data.LevelId != null) {
                        setTimeout(function(){
                            $("#rdo_level_" + ret.Data.LevelId).prop("checked",true);
                        }, 3000);

                    }
                    $scope.Contruction.TypeId = $scope.Contruction.TypeId + "";
                    $scope.Contruction.CreateTime = $filter('JSONDate')($scope.Contruction.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                    $scope.Contruction.StartTime = $filter('JSONDate')($scope.Contruction.StartTime, 'yyyy-MM-dd HH:mm');
                    $scope.Contruction.EndTime = $filter('JSONDate')($scope.Contruction.EndTime, 'yyyy-MM-dd HH:mm');
                    if (ret.Data.RiskId != null) {
                        $scope.GetConstructionRisk(ret.Data.TypeId, ret.Data.RiskId.split(','));
                    } else {
                        $scope.GetConstructionRisk(ret.Data.TypeId);
                    }

                    if (ret.Data.SolutionId != null) {
                        $scope.GetConstructionSolution(ret.Data.TypeId, ret.Data.SolutionId.split(','));
                    } else {
                        $scope.GetConstructionSolution(ret.Data.TypeId);
                    }

                    if (ret.Data.SafetyEquId != null) {
                        $scope.GetConstructionSafeEqu(ret.Data.TypeId, ret.Data.SafetyEquId.split(','));
                    }
                    else {
                        $scope.GetConstructionSafeEqu(ret.Data.TypeId);
                    }

                    $scope.BindConstructionCompanyList('sel_companylist', $scope.Contruction.OutCompanyId);
                    $scope.BindConstructionWorkerList('sel_workerlist', ret.Data.WorkerId.split(','));
                    $scope.BindConstructionCaretaker('caretakerlist', ret.Data.Caretaker.split(','));
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.EditConstructionForm = function (model, status) {
        var riskcontrol = $("input[id^='risk_']");
        var riskid = [];
        for (let i = 0; i < riskcontrol.length; i++) {
            if ($(riskcontrol[i]).prop("checked")) {
                riskid.push(riskcontrol[i].id.replace("risk_", ''));
            }
        }

        var solutioncontrol = $("input[id^='solution_']");
        var solutionid = [];
        for (let i = 0; i < solutioncontrol.length; i++) {
            if ($(solutioncontrol[i]).prop("checked")) {
                solutionid.push(solutioncontrol[i].id.replace("solution_", ''));
            }
        }

        var equcontrol = $("input[id^='equ_']");
        var equid = [];
        for (let i = 0; i < equcontrol.length; i++) {
            if ($(equcontrol[i]).prop("checked")) {
                equid.push(equcontrol[i].id.replace("equ_", ''));
            }
        }

        if (riskid.length != 0) {
            model.RiskId = riskid.join(",");
        }
        if (solutionid.length != 0) {
            model.SolutionId = solutionid.join(",");
        }
        if (equid.length != 0) {
            model.SafetyEquId = equid.join(",");
        }
        model.WorkerId = $("#sel_workerlist").val().join(",");
        model.Caretaker = $("#caretakerlist").val().join(",");
        model.PlaceId = $("#placelist").val().join(",");

        if (model.DepLeader == null && status == 1) {
            alertify.alert("请选择部门审核人");
            return false;
        }
        model.Status = status;

        var files = document.getElementById("docfiles").files;
        $scope.Submiting = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        Upload.upload({
            method: 'POST',
            url: '../Construction/EditConstructionForm',
            data: { 'model': model, "Leader": model.DepLeader },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    location.href = "ConstructionFormPageList";
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.UploadConstruction = function (Id, iscomplete, model, page) {
        if (iscomplete == null) {
            alertify.alert("请选择是否办结");
            return false;
        }
        $scope.DataLoading = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        var files = document.getElementById("docfiles").files;
        Upload.upload({
            method: 'POST',
            url: '../Construction/UploadConstruction',
            data: { 'Id': Id, 'IsComplete': iscomplete },
            file: files
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.AlertBox("提交成功！");
                    $("#btn_close").click();
                    $scope.GetConstructionPageList(model, page);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.GetCurrentModel = function (model) {
        $scope.ConstructionModel = model;
    }

    $scope.ShowTip = function (text) {
        alertify.alert(text);
    }
    $scope.MShowTip = function (text) {
        mui.alert(text);
    }
    $scope.GetParties = function (model, page, isload) {
        $scope.DataLoading = true;
        $scope.DataList = [];
        var condition = JSON.parse(sessionStorage.getItem(window.location.pathname));
        if (isload && condition) {
            model = condition;
            $scope.SearchModel = condition;
        } else {
            sessionStorage.setItem(window.location.pathname, JSON.stringify(model));
        }
        $http({
            method: 'POST',
            url: '../Construction/GetParties',
            data: { "Name": model.Name,"Type":model.Type },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
});